﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Drawing.Printing;
using ENLibraryClass;

using System.Xml;
using System.Data.SqlClient;

using Microsoft.Office.Interop.Excel;
using System.Reflection;

namespace uacars
{
    class XML
    {
        public void ExportarExcel(DataGridView dgv)
        {
            try
            {
                string temp;
                int iColumnas = 0;
                Microsoft.Office.Interop.Excel.Application xlsApp = new Microsoft.Office.Interop.Excel.Application();
                //xlsApp.DisplayAlerts = false;
                Worksheet xlsSheet;
                Workbook xlsBook;
                xlsApp.Visible = false;

                xlsBook = xlsApp.Workbooks.Add(true);
                xlsSheet = (Worksheet)xlsBook.ActiveSheet;

                int numFilas = dgv.Rows.Count;
                int numColumnas = dgv.Columns.Count;

                for (int iCol = 0; iCol < numColumnas; iCol++)
                {
                    if (dgv.Columns[iCol].Visible == true)
                    {
                        xlsSheet.Cells[1, iCol + 1] = dgv.Columns[iCol].HeaderText;
                        iColumnas++;
                    }
                }

                for (int iRow = 0; iRow < numFilas - 1; iRow++)
                {
                    for (int iCol = 0; iCol < numColumnas; iCol++)
                    {
                        temp = dgv[iCol, iRow].Value.ToString();
                        if (dgv.Columns[iCol].HeaderText == "fechaalta" || dgv.Columns[iCol].HeaderText == "fechainicio" ||dgv.Columns[iCol].HeaderText == "fechafin" ||dgv.Columns[iCol].HeaderText == "fecharealizado" ||dgv.Columns[iCol].HeaderText == "fechafinalizado" ||dgv.Columns[iCol].HeaderText == "fecha" ||dgv.Columns[iCol].HeaderText == "hora")
                        {
                            temp = Convert.ToDateTime(temp).ToShortDateString();
                        }
                        if (dgv.Columns[iCol].Visible == true)
                        {
                            xlsSheet.Cells[iRow + 2, iCol + 1] = temp;
                        }
                    }
                }

                //Definir el rango y aplicarle un formato.
                Range rango = xlsSheet.get_Range(xlsSheet.Cells[1, 1], xlsSheet.Cells[dgv.Rows.Count + 1, iColumnas]);
                rango.Cells.AutoFormat(XlRangeAutoFormat.xlRangeAutoFormatList2, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

                xlsSheet.Columns.AutoFit(); //Ajusta ancho de todas las columnas
                xlsApp.Visible = true;
                //xlsApp.Quit();
            }

            catch
            {
                string temp = "";
            }
        }
    }
}
